package com.xxp.controller;

import cn.hutool.core.collection.CollUtil;
import com.xxp.common.JwtInterceptor;
import com.xxp.common.Result;
import com.xxp.controller.request.TravelRequest;
import com.xxp.entity.Message;
import com.xxp.entity.Travel;
import com.xxp.entity.Tuijian;
import com.xxp.service.TravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/travel")
@CrossOrigin
public class TravelController {
    @Autowired
    private TravelService travelService;

    @GetMapping("/all")
    public Result selectAll() {
        List<Travel> travels = travelService.selectAll();
        return Result.success(travels);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Travel travel = travelService.selectById(id);
        return Result.success(travel);
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result page(TravelRequest travelRequest) {
        return Result.success(travelService.listByCondition(travelRequest));
    }

    @GetMapping("/pageback")
    public Result pageback(TravelRequest travelRequest){
        return Result.success(travelService.backTravel(travelRequest));
    }

    @GetMapping("/mes/{id}")
    public Result mse(@PathVariable Integer id) {
        List<Message> list = travelService.findMes(id);
        return Result.success(messageTree(null,list));
    }

    private List<Message> messageTree(Integer mid,List<Message> messages){
        List<Message> treeList=new ArrayList<>();
        for (Message message : messages) {
            if (mid==null){
                if (message.getMid()==null){
                    treeList.add(message);
                    message.setChildren(messageTree(message.getId(),messages));
                }
            }else{
                if (mid.equals(message.getMid())){
                    treeList.add(message);
                    message.setChildren(messageTree(message.getId(),messages));
                }
            }
            if (CollUtil.isEmpty(message.getChildren())){
                message.setChildren(null);
            }
        }
        return treeList;
    }

    @PostMapping("/push")
    public Result push(@RequestBody Message message) {
        System.out.println(message);
        travelService.pushMsg(message);
        return Result.success();
    }

    @PostMapping("/recommend")
    public Result recommend( @RequestBody Tuijian tuijian) {
        travelService.updateRecommend( tuijian);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteMsg(@PathVariable Integer id){
        travelService.deleteMsg(id);
        return Result.success();
    }

    @GetMapping("/mes/all")
    public Result findAllMsg(){
        return Result.success(travelService.findAllMes());
    }


    @PostMapping("/insert")
    public Result insertTravel(@RequestBody Travel travel){
        travelService.insertTravel(travel);
        return  Result.success();
    }

    @DeleteMapping("/back/{id}")
    public Result deleteTravel(@PathVariable Integer id){
        travelService.deltetTravel(id);
        return Result.success();
    }

    @PutMapping("/update")
    public Result updateTravel(@RequestBody Travel travel){
        travelService.updateTravel(travel);
        return Result.success();
    }

    @GetMapping("/TravelName")
    public Result TravelName(@PathVariable Integer id){
        Travel travel = travelService.TravelName(id);
        return Result.success(travel);
    }


    @GetMapping("/usermes/{id}")
    public Result findAllMesByUser(@PathVariable Integer id){
        List<Message> allMesByUser = travelService.findAllMesByUser(id);
        return Result.success(allMesByUser);
    }
}
